package com.ryx.controller;

import com.ryx.entity.SysUser;
import com.ryx.exception.GlobalException;
import com.ryx.exception.code.BaseResponseCode;
import com.ryx.utils.DataResult;
import com.ryx.vo.req.TestReqVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.ArrayList;

/**
 * @BelongsPackage: com.ryx.controller
 * @Author: 容永轩
 * @CreateTime: 2020-11-03
 * @Description:
 */
@RestController
@RequestMapping("/swagger")
@Api(tags = "测试接口")
public class TestController {


    @GetMapping("/type")
    public DataResult testGlobalException(@RequestParam String type){
        if (!type.equals("1")){
            /*
            流程： 先枚举类中的参数，1.调用构造方法，public GlobalException(ResponseCodeInterface responseCodeInterface){}，通过枚举类的接口get，set拿到参数
                                  2.其次调用构造方法 public GlobalException(int code, String msg) {}，赋值
                                  3，因为我们全局管理异常，在RestExceptionHandler类中捕获到这个异常，调用    public DataResult handleGlobalException(GlobalException e){}
                                  4.并return DataResult.getResult(e.getCode(),e.getMsg());
                                  5.调用我们统一封装的返回结果类中的 DataResult.getResult(e.getCode(),e.getMsg()) 方法
             */
            throw new GlobalException(BaseResponseCode.DATA_ERROR);
        }

        return  DataResult.success(type);
    }

    @GetMapping("/test")
    @ApiOperation(value = "试一试")
    public String test(){

        return "测试success";
    }

    @GetMapping("/test/data")
    @ApiOperation(value = "测试响应格式接口")
    public DataResult<String> test2(){

        SysUser user = new SysUser();
        user.setUsername("ryx");
        user.setPhone("123456");

        SysUser user1 = new SysUser();
        user1.setPhone("23141");
        user1.setUsername("ryz");

        ArrayList<Object> list = new ArrayList<>();
        list.add(user);
        list.add(user1);

//        int i = 1/0;

        return DataResult.success(list);

//        return DataResult.success(user);
//        return DataResult.success("xxxxxxxx");
    }

//     Hibernate Validator Test
    @PostMapping("/test/valid")
    public DataResult testValid(@RequestBody @Valid TestReqVo testReqVo){
        return DataResult.success(testReqVo);
    }


}
